package edu.nf.ch07.demo1.service;

import edu.nf.ch07.container.ContainerFactory;

import java.math.BigDecimal;

/**
 * @author CSY
 * @date 2025 - 04 - 17
 * 支付上下文，负责选择合适的支付策略来进行支付
 */
public class PaymentContext {
    //不允许写在这里 - 这里仅仅只是测试
    private final ContainerFactory factory = new ContainerFactory("edu.nf.ch07.pay");

    private Payment payment;

    /**
     * 构造方法传入一个支付类型 - 来决定实例化某一种支付策略
     * @param type 支付类型
     */
    public PaymentContext(String type) {
        try{
            payment = factory.getBean(type);
        }catch (Exception e) {
            throw new RuntimeException("暂不支持此支付方式...");
        }
        /*if (type.equals("alipay")){
            payment = new AliPay();
        }else if (type.equals("wechat")){
            payment = new WechatPay();
        }else{
            throw new RuntimeException("暂不支持此支付方式...");
        }*/
    }

    public void pay(BigDecimal money) {
        payment.pay(money);
    }

}
